﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFM.API;
using CFM.DTO;
using CFM.Common;
using System.Text.RegularExpressions;

namespace CFM
{
    public partial class frmUnit : Form
    {
        private UnitAPI _api = null;        
        public frmUnit()
        {
            InitializeComponent();
            _api = new UnitAPI();            
        }

        private void frmUnit_Load(object sender, EventArgs e)
        {
            EnableControl(false);

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnSave.Enabled = false;
            
            BindDataGridView();
        }

        private void BindDataGridView()
        {
            grvUnit.DataSource = _api.GetAll();             
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            EnableControl(true);
            ResetControl();

            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;            
        }

       
        private void txtUnitName_TextChanged(object sender, EventArgs e)
        {
            if (txtUnitId.Enabled == true)
            {
                txtUnitId.Text = MethodHelper.GetIdFromName(txtUnitName.Text);                
            }
        }

        private void grvUnit_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) 
                return;
            Unit unit = grvUnit.Rows[e.RowIndex].DataBoundItem as Unit;
            txtUnitId.Text = unit.UnitId;
            txtUnitName.Text = unit.UnitName;
            EnableControl(false);
            MethodHelper.ClearErrorProvider();

            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            btnSave.Enabled = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckBeforeSave()) 
                return;
            Unit unit = new Unit();
            unit.UnitId = txtUnitId.Text;
            unit.UnitName = txtUnitName.Text;
            if (txtUnitId.Enabled)
                _api.Create(unit);
            else
                _api.Update(unit);
            BindDataGridView();
            ResetControl();
            MethodHelper.ClearErrorProvider();

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (txtUnitId.Text.Length > 0)
            {
                _api.Delete(txtUnitId.Text.Trim());
                BindDataGridView();
                ResetControl();

                btnDelete.Enabled = false;
                btnEdit.Enabled = false;
            }
        }

        private void ResetControl()
        {
            txtUnitName.Text = string.Empty;
            txtUnitId.Text = string.Empty;
            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;         
            txtUnitName.Focus();
            MethodHelper.ClearErrorProvider();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtUnitId.Text.Length > 0)
            {
                EnableControl(true);
                txtUnitId.Enabled = false;
                MethodHelper.ClearErrorProvider();

                btnSave.Enabled = true;
            }
        }

        private void EnableControl(bool flag)
        {
            txtUnitId.Enabled = flag;
            txtUnitName.Enabled = flag;
        }

        private bool CheckBeforeSave()
        {
            bool flag = true;
            if (txtUnitId.Enabled && txtUnitId.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtUnitId, Const.EMPTY_MESSAGE);
                flag = false;
            }
            if (txtUnitName.Enabled && txtUnitName.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtUnitName, Const.EMPTY_MESSAGE);                
                
                flag = false;
            }
            if (!txtUnitName.Enabled && !txtUnitId.Enabled)
                flag = false;
            return flag;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
       
    }
}
